package com.xiaoyu.lanling.feature.fate.model

import android.view.LayoutInflater
import android.view.View
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.util.TextViewUtil
import com.xiaoyu.lanling.widget.flipper.ViewFlipper2
import kotlinx.android.synthetic.main.fate_on_tv_item.view.*

class TvFlipperAdapter : ViewFlipper2.ViewFlipperAdapter<TvFlipItem>() {
    val dataList = mutableListOf<TvFlipItem>()

    fun setData(datas: List<TvFlipItem>) {
        this.dataList.clear()
        this.dataList.addAll(datas)
        notifyDataSetChanged()
    }

    fun insert(position: Int, item: TvFlipItem) {
        this.dataList.add(position, item)
        notifyItemInsert(position)
    }

    override fun create(model: TvFlipItem): View {
        val rootView = LayoutInflater.from(AppContext.getContext()).inflate(R.layout.fate_on_tv_item, null)
        val textView = rootView.on_tv_message
        TextViewUtil.stripUnderlinesAndClickable(textView, model.content)
        return rootView
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun getItem(position: Int): TvFlipItem {
        return dataList[position]
    }

    override fun getItemDuration(position: Int): Long {
        return getItem(position).lockScreenDurationSec * 1000L
    }
}